<template>
    <!--    <div>载体</div>-->
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true">
            <el-form-item label="组盘名称" prop="title">
                <el-input v-model="queryParams.name" placeholder="请输入组盘名称" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8" ref="refbut">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd">添加</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-download" @click="handleExport">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table ref="table" v-loading="loading" :data="dataList" :height="tableHeight"
            @selection-change="handleSelectionChange" border stripe>
            <el-table-column type="selection" width="50" align="center" fixed="left" />
            <el-table-column label="组盘状态" prop="state" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="组盘名称" prop="name" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="数量" prop="quantity" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="盘规格大小（MB）" prop="size" align="center">
                <template slot-scope="scope">
                    <span>{{ scope.row.size + 'MB' }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button plain type="warning" size="mini" @click="handleZuPanGz(scope.row)">组盘规则</el-button>
                    <el-button plain type="success" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button plain type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 载体添加及修改 -->
        <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body :before-close="cancelForm">
            <div>
                <el-form ref="form" :rules="rules" :model="form" label-width="120px">
                    <el-form-item label="组盘状态" prop="state">
                        <el-select v-model="form.state" placeholder="请选择" clearable style="width: 100%">
                            <el-option v-for="item in stateList" :key="item.value" :label="item.label"
                                :value="item.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="组盘名称" prop="name">
                        <el-input v-model="form.name" placeholder="请输入组盘名称"></el-input>
                    </el-form-item>
                    <el-form-item label="数量" prop="quantity">
                        <el-input v-model="form.quantity" placeholder="请输入数量" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="组盘规格大小" prop="size">
                        <el-input-number v-model="form.size" controls-position="right" placeholder="组盘规格大小" :min="1"
                            style="width: 88%;" type="number"></el-input-number> (MB)
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancelForm">取 消</el-button>
            </div>
        </el-dialog>

        <zupanGZ v-if="gzOpen" :gzOpen="gzOpen" @closeZPGZDialog="closeZPGZDialog" :plateId="plateId"></zupanGZ>
    </div>
</template>

<script>
import estimateTableHeight from "@/mixins/estimateTableHeight";

import { getTableList, addItem, yqSetPlateId, deleteId,yqSetPlateDelete,editItem} from "@/api/cqbc/resourceManagement/yqSetPlate.js"
import zupanGZ from "@/views/cqbc/resourceManagement/yqSetPlate/zupanGZ.vue"
export default {
    mixins: [estimateTableHeight], // 混入方法
    archive_Dicts: ['CARRIER_TYPE'],
    components:{
        zupanGZ
    },
    data() {
        return {
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            dataList: [],
            total: 0,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name:undefined
            },

            title: '',
            open: false,
            // 表单参数
            form: {
                state: undefined,//组盘状态
                name: undefined, //组盘名称
                quantity: undefined, //组盘数量
                size: undefined //盘柜大小MB
            },
            ztlxNameOption: [],
            // 表单校验
            rules: {
                name: [
                    { required: true, message: '请输入组盘名称', trigger: 'blur' },
                ],
                state: [
                    { required: true, message: '请输入组盘状态', trigger: 'blur' },
                ],
                quantity: [
                    { required: true, message: '请输入组盘数量', trigger: 'blur' },
                ],
                size: [
                    { required: true, message: '请输入 盘规格大小', trigger: 'blur' },
                ],
            },
            //组盘状态
            stateList: [
                { name: "良好", value: "良好" },
                { name: "非常好", value: "非常好" },
                { name: "超级好", value: "超级好" },
                { name: "正常", value: "正常" }
            ],
            gzOpen:false,
            plateId:undefined
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList() {
            getTableList(this.queryParams).then(response => {
                this.dataList = response.rows
                this.total = response.total
                this.loading = false
            })
        },

        handleQuery(){
            this.getList();
        },
        resetQuery(){
            this.queryParams= {
                pageNum: 1,
                pageSize: 10,
                name:undefined
            },
            this.getList();
        },
        table_index(index) {
            return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        reset() {
            this.form = {
                state: undefined,//组盘状态
                name: undefined, //组盘名称
                quantity: undefined, //组盘数量
                size: undefined //盘柜大小MB
            }
        },
        // 新增
        handleAdd() {
            this.resetForm('form')
            this.reset()
            this.open = true
            this.title = '新增载体'
        },
        // 修改
        handleUpdate(row) {
            this.resetForm('form')
            this.reset()
            const id = row.id || this.ids
            yqSetPlateId(id).then(response => {
                this.form = response.data
                this.title = '修改载体'
                this.open = true
            })
        },
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != undefined) {
                        // this.form.ztlxName = this.ztlxNameOption.find(item => item.value == this.form.ztlxId).label
                        editItem(this.form).then(response => {
                            this.$modal.msgSuccess(response.msg)
                            this.open = false
                            this.getList()
                        })
                    } else {
                        // this.form.ztlxName = this.ztlxNameOption.find(item => item.value == this.form.ztlxId).label

                        addItem(this.form).then(response => {
                            this.$modal.msgSuccess(response.msg)
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        cancelForm() {
            this.open = false
        },
        // 删除
        handleDelete(row) {
            var id = row.id || this.ids
           
            this.$modal.confirm('是否确认删除组盘名称为"' + row.name + '"的数据项？').then(() => {
                yqSetPlateDelete(id).then(res => {
                    if (res.code == "200") {
                        this.$modal.msgSuccess("删除成功");
                        this.getList();
                    }
                })
            }).catch(() => { });
        },

        //载体检测
        handleDetection(row) {
            this.$modal.loading("加载中...")
            this.$modal.confirm('是否要进行检测').then(function () { }).then(() => {
                detection({
                    code: row.code,
                    name: row.name,
                    savePath: row.savePath
                }).then(res => {
                    if (res.code == "200") {
                        this.$modal.closeLoading();
                        this.$modal.msgSuccess("检测成功");
                        this.getList();
                    }
                })
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            var ids=this.ids.toString();
            if(!this.multiple){
            this.download('/cqbc/yqSetPlate/export',{...{ids:ids}}, `type_${new Date().getTime()}.xlsx`)
            }else{
                this.$message({ message: '请选择要导出的数据!', type: 'warning' })
            }
        },


        handleZuPanGz(row){
            this.plateId=row
            this.gzOpen=true
        },
        closeZPGZDialog(){
            this.gzOpen=false;
        },
    }
}
</script>

<style></style>
